<?php

/*
Copyright 2007,2008 Benjamin V King

BH Widgets is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

BH Widgets is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with BH Widgets; if not, write to the Free Software Foundation,
Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

require_once('BH_Widget_Data.php');
require_once('BH_Widget.php');

class BH_Widget_Trashcan {
	var $errorMsg;

	
	function admin_head () {
		$bh_widgets_url = get_settings('siteurl');
		
		if(substr($bh_widgets_url, -1, 1) != '/') {
			$bh_widgets_url .= '/';
		}
			
		$bh_widgets_url .= 'wp-content/plugins/bh-widgets/';

		echo '<link rel="stylesheet" href="'.$bh_widgets_url.'/bh-widget-admin.css" type="text/css" />'."\n";
	}
	
	function admin_page() {
		global $wpdb, $bhwd;

		$submit = $_POST['submit'];
		$widget_ids = (array) $_POST['widget_id'];
		
		if ($submit == "Delete") {
			$wpdb->query("delete from ". $wpdb->widgets ." where widget_id in ('". join("','", $widget_ids) ."')");
			
		} else if ($submit =="Restore") {
			foreach($widget_ids as $widget_id)
				$bhwd->set_widget_delete($widget_id, false);

		}


		echo "<div id=\"bh-widgets-trashcan\">\n";
		echo "<h2>BH Widgets Trashcan</h2>\n";

		if ($this->errorMsg) {
			echo '<h3 style="color:red;">' . $this->errorMsg . '</h3>';
		}

		if ($widgets = $bhwd->get_trashcan()) {
		
			uasort($widgets, create_function('$x,$y', 'return strcasecmp($x->widget_name, $y->widget_name);'));
		
			echo "<div>\n";
		
			echo "<p>";
			echo "<span class=\"widget-name\">Name</span>";
			echo "</p><br/>\n";
		
			echo "<form method=\"POST\" action=\"\">\n";
			wp_nonce_field('update-themes');
			echo '<input type="hidden" name="page" value="bh-widgets/BH_Widget_Trashcan.php"/>'. "\n";
			echo '<input type="hidden" name="page_options" value="page,widget_id,widget_name,widget_class,widget_delete" />'."\n";
			
			
			foreach ($widgets as $id => $widget) {
				if ($widget_ids && !array_search($id, $widget_ids))
					continue;
					
				echo "<p>\n";
				echo "<span class=\"widget-name\"><label>". $widget->widget_name ."</label></span>\n";
				echo "<span class=\"widget-delete\"><input type=\"checkbox\" name=\"widget_id[]\" value=\"" .$widget->widget_id."\"/></span>\n";
				echo "</p><br/>\n";
			}
				
			echo "</div>\n";


			echo '<p style="line-height: 30px;">'. "\n";
			echo "<span class=\"submit\"><input name=\"submit\" type=\"submit\" value=\"". __('Restore') ."\" /></span>\n";
			echo "<span class=\"submit\"><input name=\"submit\" type=\"submit\" value=\"". __('Delete') ."\" /></span>\n";
			echo "</p>\n";
		
			echo '</form>'. "\n";
		
		} else {
			echo '<h3 style="color:red;">Trashcan empty</h3>';
		}

		echo "</div><!-- bh-widgets-trashcan -->\n";
	}

	
	function admin_menu() {
		$page = add_theme_page('BH Widgets Trashcan', 'BH Widgets Trashcan', 8, __FILE__ ,array($this, 'admin_page'));
		add_action('admin_head-'. $page, array($this, 'admin_head'));
	}

	function widgets_init() {
		global $bhwd;
		
		if (!$bhwd) return;

		add_action('admin_menu', array($this, 'admin_menu'));
	}
}

?>
